﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC.PipeSetting
{
	public class PipeSettingManager
	{
		public static void Execute(Autodesk.Revit.DB.Document doc)
		{
			try
			{
				PipeSettingManager.document = doc;
				FrmPipeSetting frmPipeSetting = new FrmPipeSetting(PipeSettingManager.GetPipingSystemType(PipeSettingManager.document));
				if (frmPipeSetting.ShowDialog() == DialogResult.OK)
				{
					PipeSettingManager.LoadPipeSystemType(PipeSettingManager.document, frmPipeSetting.PipingTypeList);
					PipeSettingManager.document.Regenerate();
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static List<SystemTypeInfo> GetPipingSystemType(Autodesk.Revit.DB.Document doc)
		{
			bool flag = false;
			string text = string.Empty;
			string pipeType = string.Empty;
			string groupName = string.Empty;
			int groupSortIndex = -1;
			IEnumerable<PipingSystemType> pipingSystem = PipeSettingManager.GetPipingSystem(doc);
			List<SystemTypeInfo> list = new List<SystemTypeInfo>();
			foreach (PipingSystemType pipingSystemType in pipingSystem)
			{
				SystemTypeInfo systemTypeInfo = new SystemTypeInfo();
				systemTypeInfo.Abbreviation = pipingSystemType.Abbreviation;
				systemTypeInfo.SysType = pipingSystemType.Name;
				systemTypeInfo.SysCategory = pipingSystemType.get_Parameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString();
				systemTypeInfo.LineColor = SystemManageHelper.getRGB(pipingSystemType);
				systemTypeInfo.LinePattern = PipeSettingManager.GetLinePattern(pipingSystemType);
				systemTypeInfo.LineWeight = SystemManageHelper.getLineWeight(pipingSystemType);
				IDictionary<string, string> pipingTypeExtInfo = PipeSettingManager.GetPipingTypeExtInfo(pipingSystemType);
				int sortIndex;
				if (pipingTypeExtInfo != null)
				{
					if (pipingTypeExtInfo.ContainsKey(pipingSystemType.Id.IntegerValue.ToString()))
					{
						flag = true;
						text = pipingTypeExtInfo[pipingSystemType.Id.IntegerValue.ToString()];
						pipeType = text.Split(new char[]
						{
							';'
						})[0];
						groupName = text.Split(new char[]
						{
							';'
						})[1];
						sortIndex = Convert.ToInt32(text.Split(new char[]
						{
							';'
						})[2]);
						groupSortIndex = Convert.ToInt32(text.Split(new char[]
						{
							';'
						})[3]);
					}
					else
					{
						pipeType = PipeSettingManager.GetPipeTypeByPiping(systemTypeInfo.SysType);
						groupName = PipeSettingManager.GetGroupName(systemTypeInfo.SysType);
						sortIndex = PipeSettingManager.SetSortIndex(list, groupName, false);
					}
				}
				else
				{
					pipeType = PipeSettingManager.GetPipeTypeByPiping(systemTypeInfo.SysType);
					groupName = PipeSettingManager.GetGroupName(systemTypeInfo.SysType);
					sortIndex = PipeSettingManager.SetSortIndex(list, groupName, false);
				}
				systemTypeInfo.PipeType = pipeType;
				systemTypeInfo.GroupName = groupName;
				systemTypeInfo.SortIndex = sortIndex;
				systemTypeInfo.GroupSortIndex = groupSortIndex;
				systemTypeInfo.IsParent = false;
				list.Add(systemTypeInfo);
			}
			IList<string> list2 = (from x in list
			group x by x.GroupName into x
			select x.Key).ToList<string>();
			if (flag)
			{
				IEnumerator<string> enumerator2 = list2.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						string item = enumerator2.Current;
						int groupSortIndex2 = list.FirstOrDefault((SystemTypeInfo x) => !x.IsParent && x.GroupName == item).GroupSortIndex;
						list.Add(new SystemTypeInfo
						{
							IsParent = true,
							SysType = item,
							SortIndex = groupSortIndex2
						});
					}
					return list;
				}
			}
			new List<string>();
			string text2 = string.Empty;
			int num = 0;
			int i;
			int j;
			for (i = 0; i < PipeSettingManager.groupOn.Length; i = j + 1)
			{
				text2 = list2.FirstOrDefault((string x) => x == PipeSettingManager.groupOn[i]);
				if (!string.IsNullOrEmpty(text2))
				{
					list.Add(new SystemTypeInfo
					{
						IsParent = true,
						SysType = text2,
						SortIndex = num++
					});
				}
				j = i;
			}
			num = (from x in list
			where x.IsParent
			select x).Count<SystemTypeInfo>();
			IEnumerator<string> enumerator3 = list2.GetEnumerator();
			{
				while (enumerator3.MoveNext())
				{
					string item = enumerator3.Current;
					text2 = PipeSettingManager.groupOn.FirstOrDefault((string x) => x == item);
					if (string.IsNullOrEmpty(text2))
					{
						list.Add(new SystemTypeInfo
						{
							IsParent = true,
							SysType = text2,
							SortIndex = num++
						});
					}
				}
			}
            IEnumerator<SystemTypeInfo> enumerator4 = ((IEnumerable<SystemTypeInfo>)(from x in list
                                                                                     where x.IsParent
                                                                                     select x).ToList<SystemTypeInfo>()).GetEnumerator();
			{
				while (enumerator4.MoveNext())
				{
					SystemTypeInfo item = enumerator4.Current;
					foreach (SystemTypeInfo systemTypeInfo2 in ((IEnumerable<SystemTypeInfo>)(from x in list
					where !x.IsParent && x.GroupName == item.SysType
					select x).ToList<SystemTypeInfo>()))
					{
						systemTypeInfo2.GroupSortIndex = item.SortIndex;
					}
				}
			}
			return list;
		}

		private static int SetSortIndex(IList<SystemTypeInfo> collection, string groupName, bool isGroupSortIndex)
		{
			int result = -1;
			if (collection == null || string.IsNullOrEmpty(groupName))
			{
				return result;
			}
			if (isGroupSortIndex)
			{
				IEnumerable<IGrouping<string, SystemTypeInfo>> source = from x in collection
				group x by x.GroupName;
				if (source.Contains((IGrouping<string, SystemTypeInfo> x) => x.Key == groupName))
				{
					SystemTypeInfo[] array = source.FirstOrDefault((IGrouping<string, SystemTypeInfo> x) => x.Key == groupName).ToArray<SystemTypeInfo>();
					result = ((array.Count<SystemTypeInfo>() <= 0) ? -1 : array[0].GroupSortIndex);
				}
				else
				{
					result = source.Count<IGrouping<string, SystemTypeInfo>>();
				}
			}
			else
			{
				result = (from x in collection
				where !x.IsParent && x.GroupName == groupName
				select x).Count<SystemTypeInfo>();
			}
			return result;
		}

		private static string GetPipeTypeByPiping(string pipingType)
		{
			string text = string.Empty;
			IList<string> pipeTypeNames = PipeSettingManager.GetPipeTypeNames();
			if (string.IsNullOrEmpty(pipingType) || pipeTypeNames.Count <= 0)
			{
				return text;
			}
			if (pipingType.Contains("给") && !pipingType.Contains("热") && !pipingType.Contains("消") && !pipingType.Contains("喷") && !pipingType.Contains("饮") && !pipingType.Contains("幕") && !pipingType.Contains("炮") && !pipingType.Contains("雨淋") && !pipingType.Contains("中水"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PPR"));
			}
			else if (pipingType.Contains("热") || pipingType.Contains("中水"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PPR"));
			}
			else if (pipingType.Contains("冷凝") || pipingType.Contains("直饮"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PPR"));
			}
			else if (pipingType.Contains("循环供") || pipingType.Contains("循环回"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PPR"));
			}
			else if ((pipingType.Contains("污") || pipingType.Contains("废")) && !pipingType.Contains("压"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PVC"));
			}
			else if (pipingType.Contains("雨") || pipingType.Contains("通气"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("PVC"));
			}
			else if (pipingType.Contains("消") || (pipingType.Contains("压") && (pipingType.Contains("污") || pipingType.Contains("废"))))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("热镀锌钢") && x.Contains("卡箍") && !x.Contains("丝接"));
			}
			else if (pipingType.Contains("预作用喷淋") || (pipingType.Contains("喷") && !pipingType.Contains("雾")))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("热镀锌钢") && x.Contains("卡箍") && x.Contains("丝接"));
			}
			else if (pipingType.Contains("干式消防"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("热镀锌钢") && x.Contains("卡箍") && !x.Contains("丝接"));
			}
			else if (pipingType.Contains("其他"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("热镀锌钢") && x.Contains("卡箍") && !x.Contains("丝接"));
			}
			else if (pipingType.Contains("气") || pipingType.Contains("液") || pipingType.Contains("冷媒"))
			{
				text = pipeTypeNames.FirstOrDefault((string x) => x.Contains("铜") && x.Contains("焊接"));
			}
			if (string.IsNullOrEmpty(text))
			{
				text = pipeTypeNames.FirstOrDefault<string>();
			}
			return text;
		}

		public static List<SystemTypeInfo> CreateGroupNode(IDictionary<string, IList<SystemTypeInfo>> groupList)
		{
			List<SystemTypeInfo> list = new List<SystemTypeInfo>();
			int num = 0;
			foreach (KeyValuePair<string, IList<SystemTypeInfo>> keyValuePair in groupList)
			{
				SystemTypeInfo item = new SystemTypeInfo
				{
					SysType = keyValuePair.Key,
					SortIndex = num,
					IsParent = true
				};
				list.Add(item);
				int num2 = 0;
				foreach (SystemTypeInfo systemTypeInfo in keyValuePair.Value)
				{
					systemTypeInfo.SortIndex = num2;
					systemTypeInfo.IsParent = false;
					num2++;
					list.Add(systemTypeInfo);
				}
				num++;
			}
			return list;
		}

		public static string GetExtPipeType(string systemType, IDictionary<string, string> extInfos)
		{
			if (string.IsNullOrEmpty(systemType))
			{
				return "";
			}
			if (extInfos == null || !extInfos.ContainsKey(systemType))
			{
				return "";
			}
			string text = (extInfos == null) ? "" : extInfos[systemType];
			if (!string.IsNullOrEmpty(text))
			{
				return text.Split(new char[]
				{
					';'
				})[0];
			}
			return text;
		}

		public static string GetGroupName(string systemType)
		{
			if (string.IsNullOrEmpty(systemType))
			{
				return "";
			}
			IList<string> list = (from x in PipeSettingManager.groupOn
			where systemType.Contains(x)
			select x).ToList<string>();
			if (list.Count == 1)
			{
				return list.First<string>();
			}
			return PipeSettingManager.PipingMatchRule(systemType);
		}

		public static string GetGroupName(string systemType, IDictionary<string, string> extInfos)
		{
			if (string.IsNullOrEmpty(systemType))
			{
				return "";
			}
			if (extInfos != null && extInfos.ContainsKey(systemType))
			{
				string text = (extInfos == null) ? "" : extInfos[systemType];
				if (!string.IsNullOrEmpty(text))
				{
					return text.Split(new char[]
					{
						';'
					})[1];
				}
			}
			IList<string> list = (from x in PipeSettingManager.groupOn
			where systemType.Contains(x)
			select x).ToList<string>();
			if (list.Count == 1)
			{
				return list.First<string>();
			}
			return PipeSettingManager.PipingMatchRule(systemType);
		}

		private static string PipingMatchRule(string systemType)
		{
			string result = string.Empty;
			if (systemType.Contains("给") && !systemType.Contains("热") && !systemType.Contains("消") && !systemType.Contains("喷") && !systemType.Contains("饮") && !systemType.Contains("幕") && !systemType.Contains("炮") && !systemType.Contains("雨淋") && !systemType.Contains("中水"))
			{
				result = "给水";
			}
			else if (systemType.Contains("热") && !systemType.Contains("回"))
			{
				result = "热给水";
			}
			else if (systemType.Contains("热") && systemType.Contains("回"))
			{
				result = "热回水";
			}
			else if (systemType.Contains("中水"))
			{
				result = "中水";
			}
			else if (systemType.Contains("污") && !systemType.Contains("压"))
			{
				result = "污水";
			}
			else if (systemType.Contains("废") && !systemType.Contains("压"))
			{
				result = "废水";
			}
			else if (systemType.Contains("雨"))
			{
				result = "雨水";
			}
			else if (systemType.Contains("消"))
			{
				result = "消火栓";
			}
			else if (systemType.Contains("喷") && !systemType.Contains("雾"))
			{
				result = "喷淋";
			}
			else if (systemType.Contains("通气"))
			{
				result = "通气";
			}
			else if (systemType.Contains("冷凝"))
			{
				result = "冷凝水";
			}
			else if (systemType.Contains("直饮"))
			{
				result = "直饮水";
			}
			else if (systemType.Contains("循环供"))
			{
				result = "循环供";
			}
			else if (systemType.Contains("循环回"))
			{
				result = "循环回";
			}
			else if (systemType.Contains("压") && systemType.Contains("污"))
			{
				result = "压力污水";
			}
			else if (systemType.Contains("压") && systemType.Contains("废"))
			{
				result = "压力废水";
			}
			else if (systemType.Contains("预作用喷淋"))
			{
				result = "预作用喷淋";
			}
			else if (systemType.Contains("干式消防"))
			{
				result = "干式消防";
			}
			else
			{
				result = "其他";
			}
			return result;
		}

		public static IDictionary<SystemTypeInfo, IList<SystemTypeInfo>> GroupAndSort(IList<SystemTypeInfo> toList)
		{
			if (toList == null || toList.Count <= 0)
			{
				return null;
			}
			IDictionary<SystemTypeInfo, IList<SystemTypeInfo>> dictionary = new Dictionary<SystemTypeInfo, IList<SystemTypeInfo>>();
            IEnumerator<SystemTypeInfo> enumerator = ((IEnumerable<SystemTypeInfo>)(from x in toList
                                                                                    where x.IsParent
                                                                                    orderby x.SortIndex
                                                                                    select x).ToList<SystemTypeInfo>()).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					SystemTypeInfo parent = enumerator.Current;
					IList<SystemTypeInfo> list = (from x in toList
					where x.GroupName == parent.SysType && !x.IsParent
					orderby x.SortIndex
					select x).ToList<SystemTypeInfo>();
					if (list.Count > 0)
					{
						dictionary.Add(parent, list);
					}
				}
			}
			return dictionary;
		}

		public static IDictionary<string, IList<SystemTypeInfo>> GroupSystemType(IList<SystemTypeInfo> stdList)
		{
			if (stdList == null)
			{
				return null;
			}
			IDictionary<string, IList<SystemTypeInfo>> dictionary = new Dictionary<string, IList<SystemTypeInfo>>();
            IEnumerator<SystemTypeInfo> enumerator = ((IEnumerable<SystemTypeInfo>)(from x in stdList
                                                                                    where x.IsParent
                                                                                    orderby x.SortIndex
                                                                                    select x).ToList<SystemTypeInfo>()).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					SystemTypeInfo group = enumerator.Current;
					List<SystemTypeInfo> list = (from x in stdList
					where !x.IsParent && x.GroupName.Equals(@group.SysType)
					orderby x.SortIndex
					select x).ToList<SystemTypeInfo>();
					if (list.Count > 0)
					{
						dictionary.Add(group.SysType, list);
					}
				}
			}
			return dictionary;
		}

		public static IOrderedDictionary GroupSystemType1(IList<SystemTypeInfo> stdList)
		{
			if (stdList == null)
			{
				return null;
			}
			IOrderedDictionary orderedDictionary = new OrderedDictionary();
			string[] array = PipeSettingManager.groupOn;
			for (int i = 0; i < array.Length; i++)
			{
				string groupName = array[i];
				List<SystemTypeInfo> list = (from x in stdList
				where x.GroupName.Equals(groupName)
				select x).ToList<SystemTypeInfo>();
				if (list.Count > 0)
				{
					orderedDictionary.Add(groupName, list);
				}
			}
			return orderedDictionary;
		}

		private static void LoadPipeSystemType(Autodesk.Revit.DB.Document doc, List<SystemTypeInfo> destList)
		{
			if (destList == null)
			{
				return;
			}
			string empty = string.Empty;
			IList<ElementId> list = new List<ElementId>();
			IList<PipingSystemType> list2 = new List<PipingSystemType>();
			IList<SystemTypeInfo> list3 = (from x in destList
			where !x.IsParent
			select x).ToList<SystemTypeInfo>();
			IList<PipingSystemType> pipingSystem = PipeSettingManager.GetPipingSystem(doc);
			IEnumerator<PipingSystemType> enumerator = pipingSystem.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					PipingSystemType srcItem = enumerator.Current;
					if (list3.FirstOrDefault((SystemTypeInfo x) => x.SysType == srcItem.Name) == null)
					{
						list.Add(srcItem.Id);
						list2.Add(srcItem);
					}
				}
			}
			foreach (PipingSystemType item2 in list2)
			{
				pipingSystem.Remove(item2);
			}
			IEnumerator<SystemTypeInfo> enumerator2 = list3.GetEnumerator();
			{
				while (enumerator2.MoveNext())
				{
					SystemTypeInfo item = enumerator2.Current;
					PipingSystemType pipingSystemType = pipingSystem.FirstOrDefault((PipingSystemType x) => x.Name == item.SysType);
					if (pipingSystemType == null)
					{
						MEPSystemClassification mepsystemClassification = (MEPSystemClassification)Enum.Parse(typeof(MEPSystemClassification), AssistSystemType.GetPipeSystemTypeUS(item.SysCategory), true);
						pipingSystemType = PipingSystemType.Create(doc, mepsystemClassification, item.SysType);
					}
					pipingSystemType.get_Parameter(BuiltInParameter.RBS_SYSTEM_ABBREVIATION_PARAM).Set(item.Abbreviation);
					if (item.LinePattern != "默认")
					{
						pipingSystemType.LinePatternId = LinePatternElement.GetLinePatternElementByName(doc, item.LinePattern).Id;
					}
					else
					{
						pipingSystemType.LinePatternId = ElementId.InvalidElementId;
					}
					if (item.LineWeight != "默认")
					{
						pipingSystemType.LineWeight = int.Parse(item.LineWeight);
					}
					else
					{
						pipingSystemType.LineWeight = 5;
					}
					if (item.LineColor != "默认")
					{
						Autodesk.Revit.DB.Color revirColor = SystemManageHelper.getRevirColor(item.LineColor);
						pipingSystemType.LineColor = revirColor;
						new AssistSystemType().setSystemTypeMaterialColor(pipingSystemType, doc, revirColor, false, pipingSystemType.LinePatternId, pipingSystemType.LineWeight);
					}
				}
			}
			doc.Delete(list);
			doc.Regenerate();
			PipeSettingManager.BindPipingInfo(list3);
		}

		private static List<PipingSystemType> GetPipingSystem(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(PipingSystemType)).ToElements();
			List<PipingSystemType> list = new List<PipingSystemType>();
			foreach (Element element in enumerable)
			{
				PipingSystemType item = element as PipingSystemType;
				list.Add(item);
			}
			return list;
		}

		public static IList<PipeType> GetPipeTypes()
		{
			IList<PipeType> list = new List<PipeType>();
			foreach (Element element in ((IEnumerable<Element>)YJKElementFilter.GetElementByClass(typeof(PipeType), PipeSettingManager.document)))
			{
				list.Add(element as PipeType);
			}
			return list;
		}

		public static IList<string> GetPipeTypeNames()
		{
			IList<string> list = new List<string>();
			foreach (PipeType pipeType in PipeSettingManager.GetPipeTypes())
			{
				list.Add(pipeType.Name);
			}
			return list;
		}

		public static string GetLinePattern(PipingSystemType pipingSystemType)
		{
			return SystemManageHelper.getLinePattern(pipingSystemType, PipeSettingManager.document);
		}

		public static IList<string> GetLinePatternName()
		{
			IList<string> list = new List<string>();
			foreach (LinePatternElement linePatternElement in SystemManageHelper.Get_LinePatternElement(PipeSettingManager.document))
			{
				list.Add(linePatternElement.Name);
			}
			return list;
		}

		public static IList<string> GetLineWeight()
		{
			IList<string> list = new List<string>();
			for (int i = 0; i < 17; i++)
			{
				list.Add(i.ToString());
			}
			return list;
		}

		public static IList<string> GetExistNames(bool isParentNode, IList<SystemTypeInfo> sourceList)
		{
			IList<string> result = new List<string>();
			if (sourceList.Count <= 0)
			{
				return result;
			}
			return (from x in sourceList
			where x.IsParent == isParentNode
			select x.SysType).ToList<string>();
		}

		public static IDictionary<string, IList<SystemTypeInfo>> GetPipingTypeInfo(Autodesk.Revit.DB.Document doc)
		{
			PipeSettingManager.document = doc;
			return PipeSettingManager.GroupSystemType(PipeSettingManager.GetPipingSystemType(PipeSettingManager.document));
		}

		public static void AdjustComboBoxDropDownListWidth(System.Windows.Forms.ComboBox comboBoxColumn)
		{
			Graphics graphics = null;
			if (comboBoxColumn == null)
			{
				return;
			}
			try
			{
				int num = comboBoxColumn.Width;
				graphics = comboBoxColumn.CreateGraphics();
				Font font = comboBoxColumn.Font;
				int num2 = (comboBoxColumn.Items.Count > comboBoxColumn.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
				foreach (object obj in comboBoxColumn.Items)
				{
					if (obj != null)
					{
						int num3 = (int)graphics.MeasureString(obj.ToString().Trim(), font).Width + num2;
						if (num < num3)
						{
							num = num3;
						}
					}
				}
				comboBoxColumn.DropDownWidth = num;
			}
			catch
			{
			}
			finally
			{
				if (graphics != null)
				{
					graphics.Dispose();
				}
			}
		}

		public static void AddProjectInfoExt(Autodesk.Revit.DB.Document doc, IDictionary<string, string> data)
		{
			if (data == null || data.Count <= 0)
			{
				return;
			}
			ProjectInfo projectInformation = doc.ProjectInformation;
			bool flag = false;
			Schema schema = null;
			foreach (Guid guid in projectInformation.GetEntitySchemaGuids())
			{
				Schema schema2 = Schema.Lookup(guid);
				if (schema2.SchemaName == "PipingTypeInfo")
				{
					flag = true;
					schema = schema2;
					break;
				}
			}
			if (!flag)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(Guid.NewGuid());
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetVendorId("ADSK");
				schemaBuilder.SetSchemaName("PipingTypeInfo");
				schemaBuilder.AddMapField("PipeTypeInfo", typeof(string), typeof(string));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("PipeTypeInfo");
			entity.Set<IDictionary<string, string>>(field, data);
			projectInformation.SetEntity(entity);
		}

		private static IDictionary<string, string> GetPipingTypeExtInfo(Element element)
		{
			Schema schema = Schema.Lookup(new Guid("720080CD-DA99-40DC-9415-E53F280AA1F5"));
			if (schema == null)
			{
				return null;
			}
			Entity entity = element.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return null;
			}
			return entity.Get<IDictionary<string, string>>("PipingTypeInfo");
		}

		private static IDictionary<string, string> GetProjectInfoExt(Autodesk.Revit.DB.Document doc)
		{
			IDictionary<string, string> result;
			try
			{
				IDictionary<string, string> dictionary = null;
				ProjectInfo projectInformation = doc.ProjectInformation;
				Schema schema = null;
				foreach (Guid guid in projectInformation.GetEntitySchemaGuids())
				{
					Schema schema2 = Schema.Lookup(guid);
					if (schema2.SchemaName == "PipingTypeInfo")
					{
						schema = schema2;
						break;
					}
				}
				if (schema != null)
				{
					dictionary = projectInformation.GetEntity(schema).Get<IDictionary<string, string>>("PipeTypeInfo");
				}
				result = dictionary;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private static void BindPipingInfo(IList<SystemTypeInfo> systemTypeLst)
		{
			if (systemTypeLst == null || systemTypeLst.Count <= 0)
			{
				return;
			}
			string key = string.Empty;
			string value = string.Empty;
			IEnumerator<PipingSystemType> enumerator = ((IEnumerable<PipingSystemType>)PipeSettingManager.GetPipingSystem(PipeSettingManager.document)).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					PipingSystemType item = enumerator.Current;
					SystemTypeInfo systemTypeInfo = systemTypeLst.FirstOrDefault((SystemTypeInfo x) => x.SysType == item.Name);
					if (systemTypeInfo != null)
					{
						key = item.Id.IntegerValue.ToString();
						IDictionary<string, string> dictionary = new Dictionary<string, string>();
						value = string.Format("{0};{1};{2};{3}", new object[]
						{
							systemTypeInfo.PipeType,
							systemTypeInfo.GroupName,
							systemTypeInfo.SortIndex,
							systemTypeInfo.GroupSortIndex
						});
						dictionary.Add(key, value);
						PipeSettingManager.StoreData(item, dictionary);
					}
				}
			}
		}

		public static void StoreData(Element element, IDictionary<string, string> data)
		{
			Schema schema = Schema.Lookup(new Guid("720080CD-DA99-40DC-9415-E53F280AA1F5"));
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("720080CD-DA99-40DC-9415-E53F280AA1F5"));
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetVendorId("ASDK");
				schemaBuilder.AddMapField("PipingTypeInfo", typeof(string), typeof(string));
				schemaBuilder.SetSchemaName("PipingTypeInfo");
				schema = schemaBuilder.Finish();
			}
			Entity entity = element.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				entity = new Entity(schema);
			}
			Field field = schema.GetField("PipingTypeInfo");
			if (field != null && entity != null)
			{
				entity.Set<IDictionary<string, string>>(field, data);
				element.SetEntity(entity);
			}
		}

		private static IDictionary<string, string> MatchedPipingInfo(List<SystemTypeInfo> pipingList)
		{
			if (pipingList == null || pipingList.Count <= 0)
			{
				return null;
			}
			string value = string.Empty;
			IDictionary<string, string> dictionary = new Dictionary<string, string>();
			pipingList.RemoveAll((SystemTypeInfo x) => x.IsParent);
			foreach (SystemTypeInfo systemTypeInfo in pipingList)
			{
				value = string.Format("{0};{1}", systemTypeInfo.PipeType, systemTypeInfo.GroupName);
				dictionary.Add(systemTypeInfo.SysType, value);
			}
			return dictionary;
		}

		public static void AddSourceGroups(IList<string> newGroupList)
		{
			foreach (string item in PipeSettingManager.groupOn)
			{
				if (!newGroupList.Contains(item))
				{
					newGroupList.Add(item);
				}
			}
			if (!newGroupList.Contains("其他"))
			{
				newGroupList.Add("其他");
			}
		}

		public static string GetUnifiedName(string name, IList<string> existNames)
		{
			if (existNames.Contains(name))
			{
				string name2 = name + 1;
				return PipeSettingManager.GetUnifiedName(name2, existNames);
			}
			return name;
		}

		public static bool IsExsitCreatedPiping(IList<SystemTypeInfo> pipingClassifyLst)
		{
			bool result = false;
			if (pipingClassifyLst == null || pipingClassifyLst.Count <= 0)
			{
				return result;
			}
			ICollection<Element> collection = new FilteredElementCollector(PipeSettingManager.document).OfClass(typeof(Pipe)).ToElements();
			string b = string.Empty;
			string b2 = string.Empty;
			foreach (SystemTypeInfo systemTypeInfo in pipingClassifyLst)
			{
				foreach (Element element in collection)
				{
					Pipe pipe = element as Pipe;
					if (pipe != null)
					{
						b = PipeSettingManager.document.GetElement(pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()).Name;
						b2 = pipe.GetParameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString();
						if (systemTypeInfo.SysType == b && systemTypeInfo.SysCategory == b2)
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}

		public static readonly string SystemName = "管道系统";

		public static readonly string CurProjectStyle = "当前项目";

		public static readonly string HyStandardStyle = "盈建科标准";

		public static readonly string path = string.Empty;

		private static Document document;

		private static string[] groupOn = new string[]
		{
			"给水",
			"热给水",
			"热回水",
			"中水",
			"污水",
			"废水",
			"雨水",
			"消火栓",
			"喷淋",
			"通气",
			"冷凝水",
			"直饮水",
			"循环供",
			"循环回",
			"压力污水",
			"压力废水",
			"预作用喷淋",
			"干式消防",
			"其他"
		};

		public static IList<string> PipingClassify = new List<string>
		{
			"家用冷水",
			"家用热水",
			"卫生设备",
			"湿式消防系统",
			"预作用消防系统",
			"其他消防系统",
			"循环供水",
			"循环回水",
			"干式消防系统",
			"通风孔",
			"其他"
		};
	}
}
